package com.guapi.controller;

import cn.hutool.system.oshi.CpuInfo;
import cn.hutool.system.oshi.OshiUtil;
import com.guapi.annotation.SelfController;
import com.guapi.dao.IpRecord;
import com.guapi.dao.ResponseData;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import oshi.hardware.GlobalMemory;

import javax.servlet.http.HttpServletRequest;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.concurrent.atomic.AtomicReference;

@SelfController
@RequestMapping("/system")
@ResponseBody
public class SystemInfoController extends BaseController<String> {

    @Autowired
    private RedisTemplate redisTemplate;

    @RequestMapping("/info")
    public ResponseData getSystemInfo(HttpServletRequest request) {
        responseData = new ResponseData();
        HashMap<String, Object> map = new HashMap<>();
        CpuInfo cpuInfo = OshiUtil.getCpuInfo();
        HashMap<String, Object> tmpMap = new HashMap<>();
        tmpMap.put("total", cpuInfo.getToTal());
        tmpMap.put("used", cpuInfo.getUsed());
        tmpMap.put("desc",cpuInfo.getCpuModel());
        map.put("CPU", tmpMap);
        tmpMap=new HashMap<>();
        GlobalMemory memory = OshiUtil.getMemory();
        tmpMap.put("total",memory.getTotal());
        tmpMap.put("used",memory.getTotal()- memory.getAvailable());
        AtomicReference<String> memoryDesc= new AtomicReference<>("");
        memory.getPhysicalMemory().forEach(item->memoryDesc.set(item.getManufacturer()+"\n"+memoryDesc.get()));
        tmpMap.put("desc",memoryDesc);
        map.put("MEMORY",tmpMap);
        responseData.setData(map);
        responseData.setTotal(2L);
        responseData.setSuccess(true);
        return responseData;
    }

    @RequestMapping("/ip/show")
    public ResponseData showInWebIp(HttpServletRequest request){
        responseData=new ResponseData();
        return responseData;
    }

    @RequestMapping("/ip/manage")
    public ResponseData manageIp(HttpServletRequest request){
        responseData=new ResponseData();
        ArrayList<IpRecord> ipRecords = new ArrayList<>();
        //获取最近一天的ip记录

        //获取redis缓存里的黑名单ip记录，将黑名单的ip状态设置为false
        //返回ip列表
        return responseData;
    }
}
